package com.etennis.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.etennis.pojos.ResponseResult;
import com.etennis.pojos.dto.app.RecordDTO;
import com.etennis.pojos.dto.app.RecordQueryDTO;
import com.etennis.pojos.dto.app.RecordStatusDTO;
import com.etennis.pojos.entity.Member;
import com.etennis.pojos.entity.Record;
import com.etennis.pojos.entity.Season;
import com.etennis.pojos.entity.Seasoninfo;
import com.etennis.pojos.vo.app.MatchVO;
import com.etennis.pojos.vo.app.RecordVO;
import com.etennis.service.*;
import com.etennis.utils.BeanCopyUtils;
import com.etennis.utils.RecordScoreUtils;
import com.etennis.utils.ResultPoint;
import com.etennis.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/app/record")
@Slf4j
@Api(tags = "用户端-比赛记录接口")
public class RecordController {
    @Autowired
    IRecordService iRecordService;
    @Autowired
    private IRecordService recordService;
    @Autowired
    private ISeasonInfoService seasonInfoService;
    @Autowired
    private IMemberService memberService;

    // todo redis

    @GetMapping("/unfinishedRecord")
    public ResponseResult<List<RecordVO>> unfinishedRecord(){
        return ResponseResult.okResult(recordService.unfinishedRecord());
    }
    @GetMapping("/record")
    @ApiOperation("比赛记录列表-查看比赛记录")
    public ResponseResult<List<RecordVO>> record(RecordQueryDTO recordQueryDTO){
        Member member=null;
        if(recordQueryDTO.getMemberName()!=null)
            member = memberService.getOne(new LambdaQueryWrapper<Member>().eq(recordQueryDTO.getMemberName() != null, Member::getName, recordQueryDTO.getMemberName()));
        LambdaQueryWrapper<Record> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(recordQueryDTO.getEventId()!=null,Record::getEventId,recordQueryDTO.getEventId())
                .eq(!recordQueryDTO.getFlag().equals("0")&&recordQueryDTO.getFlag()!=null,Record::getFlag,recordQueryDTO.getFlag())
                .orderByAsc(Record::getIsFinished);
        if(member!=null){
            queryWrapper.eq(member!=null,Record::getWinA1Id,member.getId()).or()
                    .eq(member!=null,Record::getWinA2Id,member.getId()) .or()
                    .eq(member!=null,Record::getLoseB1Id,member.getId()) .or()
                    .eq(member!=null,Record::getLoseB2Id,member.getId());
        }
        List<Record> list = recordService.list(queryWrapper);
        List<RecordVO> recordVOS = BeanCopyUtils.copyBeanList(list, RecordVO.class);
        for (RecordVO recordVO : recordVOS) {
            recordVO.setWinA1Name(memberService.getById(recordVO.getWinA1Id()).getName());
            recordVO.setLoseB1Name(memberService.getById(recordVO.getLoseB1Id()).getName());
            if(recordVO.getFlag().equals("2")){
                recordVO.setWinA2Name(memberService.getById(recordVO.getWinA2Id()).getName());
                recordVO.setLoseB2Name(memberService.getById(recordVO.getLoseB2Id()).getName());
            }
            recordVO.setScore(16);
        }
        return ResponseResult.okResult(recordVOS);
    }
    @PutMapping("/status")
    @ApiOperation("确认或拒绝比赛记录")
    public ResponseResult recordStatus(@RequestBody RecordStatusDTO recordStatusDTO){

        if(!recordStatusDTO.getStatus().equals("1")){
            recordService.removeById(recordStatusDTO.getId());
            return ResponseResult.okResult();
        }
        Record record=recordService.getById(recordStatusDTO.getId());
        Member member1=null;
        Member member2=null;
        Member member3=null;
        Member member4=null;
        Seasoninfo a2=null;
        Seasoninfo b2=null;
        Seasoninfo a1 = seasonInfoService.getOne(new LambdaQueryWrapper<Seasoninfo>().eq(Seasoninfo::getMemberId, record.getWinA1Id()));
        Seasoninfo b1 = seasonInfoService.getOne(new LambdaQueryWrapper<Seasoninfo>().eq(Seasoninfo::getMemberId, record.getLoseB1Id()));
        if(record.getFlag().equals("2")){
            a2 = seasonInfoService.getOne(new LambdaQueryWrapper<Seasoninfo>().eq(Seasoninfo::getMemberId, record.getWinA2Id()));
            b2=seasonInfoService.getOne(new LambdaQueryWrapper<Seasoninfo>().eq(Seasoninfo::getMemberId, record.getLoseB2Id()));
        }
        if(record.getFlag().equals("1")){
            ResultPoint singleResult = RecordScoreUtils.getSingleResult(a1.getGroupId(), b1.getGroupId(), record.getPointa(), record.getPointb());
            member1 = memberService.getById(record.getWinA1Id());
            member1.setScore(member1.getScore()+singleResult.getScoreA1());
            member1.setRestScore(member1.getRestScore()+singleResult.getScoreA1());
            member2 = memberService.getById(record.getLoseB1Id());
            member2.setScore(member2.getScore()+singleResult.getScoreB1());
            member2.setRestScore(member2.getRestScore()+singleResult.getScoreB1());
            memberService.updateById(member1);
            memberService.updateById(member2);
        }
        else {
            ResultPoint singleResult = RecordScoreUtils.getDoubleResult(a1.getGroupId(), a2.getGroupId(),b1.getGroupId(),b2.getGroupId(), record.getPointa(), record.getPointb());
            member1 = memberService.getById(record.getWinA1Id());
            member1.setScore(member1.getScore()+singleResult.getScoreA1());
            member1.setRestScore(member1.getRestScore()+singleResult.getScoreA1());
            member2 = memberService.getById(record.getLoseB1Id());
            member2.setScore(member2.getScore()+singleResult.getScoreB1());
            member2.setRestScore(member2.getRestScore()+singleResult.getScoreB1());
            member3 = memberService.getById(record.getWinA2Id());
            member3.setScore(member3.getScore()+singleResult.getScoreA2());
            member3.setRestScore(member3.getRestScore()+singleResult.getScoreA2());
            member4 = memberService.getById(record.getLoseB2Id());
            member4.setScore(member4.getScore()+singleResult.getScoreB2());
            member4.setRestScore(member4.getRestScore()+singleResult.getScoreB2());
            memberService.updateById(member1);
            memberService.updateById(member2);
            memberService.updateById(member3);
            memberService.updateById(member4);
        }

        record.setIsFinished("11");
        recordService.updateById(record);
        return ResponseResult.okResult();
    }


    @PutMapping("/update")
    @ApiOperation("修改比赛记录")
    public ResponseResult update(@RequestBody RecordDTO recordDTO){
        iRecordService.updateRecord(recordDTO);

        return ResponseResult.okResult();
    }

    @PostMapping("/upload")
    @ApiOperation("上传比赛记录")
    public ResponseResult upload(@RequestBody RecordDTO recordDTO){
        iRecordService.saveRecord(recordDTO);
        return ResponseResult.okResult();
    }
}
